# -*- coding: utf-8 -*-
from odoo import models, api, fields, tools
from odoo.exceptions import UserError
import re


class file_report(models.Model):

    _name = "proman.file_report"
    _description = "文件上报"
    _table = "proman_file_report"
    _order = "sequence"
    _inherit =  ['mail.thread', 'mail.activity.mixin', 'image.mixin']

    sequence = fields.Integer(string="排序",translate = 0)
    name = fields.Char(string="文件主题",help = '请输入文件主题',translate = 0)
    key_label_ids = fields.Many2many(string="关键词标签",comodel_name = 'proman.key_label',translate = 0)
    company_id = fields.Many2one(string="所属公司",comodel_name = 'res.company',default = lambda self:self.env.company,required = 1,translate = 0)
    project_id = fields.Many2one(string="项目名称",comodel_name = 'proman.project_base',translate = 0)
    project_stage_id = fields.Many2one(string="项目阶段",comodel_name = 'proman.stage_category',default = lambda self:self.env['proman.stage_category'].search([('name','=','可行性研究')]),translate = 0)
    file_no = fields.Char(string="文件号",translate = 0)
    date = fields.Date(string="发布日期",translate = 0)
    partner_id = fields.Many2one(string="相关单位",comodel_name = 'res.partner',domain = [('is_company','=',True)],translate = 0)
    manage_department_id = fields.Many2one(string="管理机构",comodel_name = 'hr.department',default = lambda self: self.env.user.default_department,translate = 0)
    user_id = fields.Many2one(string="部门负责人",comodel_name = 'res.users',translate = 0)
    create_department_id = fields.Many2one(string="录入部门",comodel_name = 'hr.department',translate = 0)
    url = fields.Char(string="相关链接",translate = 0)
    state = fields.Selection(string="状态",selection = [('1', '草稿'), ('2', '提交'), ('3', '审核')],default='1',translate = 0)
    image = fields.Binary(string="相关照片",translate = 0)
    memo = fields.Html(string="详细内容",translate = 0)
    attachment = fields.Many2many(string="相关附件",comodel_name = 'ir.attachment',translate = 0)

    def button1(self):
        if self.state == '1':
            self.write({'state': '2'})
        # 审核
    def button2(self):
        if self.state == '2':
            self.write({'state': '3'})
        # 退回
    def button3(self):
        if self.state == '2':
            self.write({'state': '1'})

        # 反审核
    def button4(self):
        if self.state == '3':
            self.write({'state': '2'})
    @api.model
    def create(self, vals):
        old_name = vals.get("name", "")
        if vals.get("is_copy", False) and old_name:
            vals.update({"name": old_name + "_复制"})
        return super(file_report, self).create(vals)
    def write(self, vals):
        vals['is_copy'] = False
        return super(file_report, self).write(vals)

    @api.returns('self', lambda value: value.id)
    def copy(self, default=None):
        self.ensure_one()
        default = dict(default or {})
        if 'is_copy' not in default:
            default['is_copy'] = True
        return super(file_report, self).copy(default=default)
    def unlink(self):
        for record in self:
            if record.state != '1':
                raise UserError("非草稿数据，不可删除，请取消并检查！")
        res = super(file_report, self).unlink()
        return res
